﻿using System;
using System.Windows.Forms;
using System.Diagnostics;
using WeifenLuo.WinFormsUI.Docking;

namespace VisualPureBasicIDE.CoreGUI
{
    public partial class ConsoleForm : DockContent
    {
        public ConsoleForm()
        {
            InitializeComponent();
            richTextBoxConsole.SelectionStart = richTextBoxConsole.TextLength;
        }

        private void FormConsoleKeyDown(object sender, KeyEventArgs e)
        {
            if (richTextBoxConsole.SelectionStart != 0)
            {
                if (e.KeyCode == Keys.Back && richTextBoxConsole.Text[richTextBoxConsole.SelectionStart - 1] == '>' || e.KeyCode == Keys.Left && richTextBoxConsole.Text[richTextBoxConsole.SelectionStart - 1] == '>' || e.KeyCode == Keys.Return || e.KeyCode == Keys.Enter || e.KeyCode == Keys.Up)
                {
                    e.SuppressKeyPress = true;
                }
            }
        }

        private void RichTextBoxConsoleMouseDown(object sender, MouseEventArgs e)
        {
            richTextBoxConsole.SelectionStart = richTextBoxConsole.TextLength;
            richTextBoxConsole.ReadOnly = true;
        }

        private void RichTextBoxConsoleMouseUp(object sender, MouseEventArgs e)
        {
            richTextBoxConsole.SelectionStart = richTextBoxConsole.TextLength;
            richTextBoxConsole.ReadOnly = false;
        }

        private void RichTextBoxConsolePreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
        {
            if (e.KeyCode == Keys.Enter | e.KeyCode == Keys.Return)
            {
                richTextBoxConsole.ReadOnly = true;
                if (richTextBoxConsole.Text[richTextBoxConsole.TextLength - 1] == '>')
                {
                    richTextBoxConsole.Text += "\nNo command entered\nPowCons >";
                    richTextBoxConsole.SelectionStart = richTextBoxConsole.TextLength;
                    richTextBoxConsole.ReadOnly = false;
                }
                else
                {
                    ProcessCommand(richTextBoxConsole.Text.Substring(richTextBoxConsole.Text.LastIndexOf('>') + 1));
                    richTextBoxConsole.Text += "\nPowCons >";
                    richTextBoxConsole.SelectionStart = richTextBoxConsole.TextLength;
                }
            }
        }

        private void ProcessCommand(string commandline)
        {
            if (commandline == " ") 
            {
                richTextBoxConsole.ReadOnly = false;
                return;
            }
            richTextBoxConsole.Text += "\n";
            Debug.Print(commandline);
            string command, arguments = null;
            var last = -1;
            var first = commandline.IndexOf('"');
            if (first != -1)
            {
                last = commandline.IndexOf('"', first + 1);
            }
            if (commandline.IndexOf(' ', last + 1) != -1)
            {
                command = commandline.Substring(0, commandline.IndexOf(' ', last + 1));
                arguments = commandline.Substring(commandline.IndexOf(' ', last + 1) + 1);
                Debug.Print(command + " " + arguments);
            }
            else
            {
                command = commandline;
            }
            var p = new Process
                        {
                            StartInfo =
                                {
                                    FileName = command,
                                    Arguments = arguments,
                                    UseShellExecute = false,
                                    RedirectStandardOutput = true,
                                    CreateNoWindow = true
                                }
                        };
            try
            {
                p.Start();
                richTextBoxConsole.Text += p.StandardOutput.ReadToEnd();
                p.WaitForExit();
            }
            catch (Exception ex)
            {
                richTextBoxConsole.Text += "An error occured while starting the process: " + '"' + ex.Message + '"' + '.';
            }
            richTextBoxConsole.ReadOnly = false;
        }
    }
}